package com.shitou.service;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itextpdf.text.DocumentException;
import com.shitou.dao.ProjectDao;
import com.shitou.pojo.Project;
import com.shitou.pojo.Report;
import com.shitou.pojo.ReportItem;
import com.shitou.pojo.User;
import com.shitou.util.DateUtil;
import com.shitou.util.IssuesUtil;
import com.shitou.util.JFreeCharUtil;
import com.shitou.util.ReportPdfUtil;

@Service
@Transactional
public class ProjectService {

	@Resource
	private ProjectDao projectDao;
	@Resource
	private UserService userService;
	@Resource
	private NewsService newsService;
	@Resource
	private IssuesService issuesService;
	
	public void save(Project project) {
		projectDao.save(project);
	}
	
	public Integer save(String name, String ids, User user) {
		Project project = new Project();
		project.setName(name);
		project.setCreatedate(DateUtil.getNow());
		List<User> userList = userService.findByIds(ids);
		project.setUsers(userList);
		projectDao.save(project);
		newsService.save(project, user);
		return project.getId();
	}
	
	public Project findById(int id) {
		return projectDao.findById(id);
	}

	public List<Project> findAll() {
		return projectDao.findAll();
	}
	
	public Project findLast() {
		return projectDao.findLast();
	}
	
	public List<Project> findByUser(User user) {
		List<Project> projectList = null;
		user = userService.findById(user.getId());
		if(user != null) {
			projectList = user.getProjects();
		} else {
			projectList = findAll();
		}
		return projectList;
	}

	public List<Project> getReport(String realPath) {
		List<Project> list = findAll();
		for(Project p: list) {
			DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
			dataSet.addValue(issuesService.findUndoneCount(IssuesUtil.TASK, p.getId()), "未完成", "任务");
			dataSet.addValue(issuesService.findUndoneCount(IssuesUtil.BUG, p.getId()), "未完成", "错误");
			dataSet.addValue(issuesService.findUndoneCount(IssuesUtil.IDEA, p.getId()), "未完成", "想法");
			dataSet.addValue(issuesService.findDoneCount(IssuesUtil.TASK, p.getId()), "已完成", "任务");
			dataSet.addValue(issuesService.findDoneCount(IssuesUtil.BUG, p.getId()), "已完成", "错误");
			dataSet.addValue(issuesService.findDoneCount(IssuesUtil.IDEA, p.getId()), "已完成", "想法");
			
			ChartFactory.setChartTheme(JFreeCharUtil.getTheme());
			
			JFreeChart chart = ChartFactory.createBarChart3D(p.getName(), "问题分类", "完成量", dataSet, PlotOrientation.VERTICAL, true, false, false);
			
			try {
				ChartUtilities.saveChartAsJPEG(new File(realPath+"report/project"+p.getId()+".jpg"), chart, 400, 300);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		try {
			new ReportPdfUtil().createHtmlAndPdf(realPath+"report.html", realPath+"report.pdf", realPath, list);
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	public List<Report> getReport() {
		List<Project> list = findAll();
		List<Report> reportList = new ArrayList<Report>();
		for(Project p : list) {
			Report r = new Report();
			Map<String, ReportItem> map = new HashMap<String, ReportItem>();
			ReportItem ri1 = new ReportItem();
			ri1.setUndone(issuesService.findUndoneCount(IssuesUtil.TASK, p.getId()).intValue());
			ri1.setDone(issuesService.findDoneCount(IssuesUtil.TASK, p.getId()).intValue());
			map.put("任务", ri1);
			ReportItem ri2 = new ReportItem();
			ri2.setUndone(issuesService.findUndoneCount(IssuesUtil.BUG, p.getId()).intValue());
			ri2.setDone(issuesService.findDoneCount(IssuesUtil.BUG, p.getId()).intValue());
			map.put("错误", ri2);
			ReportItem ri3 = new ReportItem();
			ri3.setUndone(issuesService.findUndoneCount(IssuesUtil.IDEA, p.getId()).intValue());
			ri3.setDone(issuesService.findDoneCount(IssuesUtil.IDEA, p.getId()).intValue());
			map.put("想法", ri3);
			r.setId(p.getId());
			r.setName(p.getName());
			r.setMap(map);
			reportList.add(r);
		}
		return reportList;
	}

}
